home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / batchut / rbsetnv1.zip / INSPATH.C < prev    next >
Text File  |  1990-04-13  |  1KB  |  66 lines

  1. /*
  2.  * inspath.c
  3.  *
  4.  * Insert directory <pathname2> before <pathname1>
  5.  *
  6.  * Returned status:
  7.  *    0    OK (including case where nothing needs to be done)
  8.  *    1    bad arguments
  9.  *
  10.  */
  11. #include <io.h>
  12. #include <string.h>
  13. #include <stdlib.h>
  14.  
  15. void exit(int status) { _exit(status); }
  16.  
  17. #define writes(s,i)  write(i,s,strlen(s));
  18.  
  19. main(int argc, char *argv[])
  20. {
  21.     int len;
  22.     char *path, *pathname1, *pathname2, *str;
  23.  
  24.     if( argc != 3)    {
  25.         writes("Usage: inspath  path_to_insert_before  new_path\n",2);
  26.         exit(1);
  27.     } else    {
  28.         len = strlen(argv[1]);
  29.         if(*(argv[1]+len-1) == ';') *(argv[1]+len-1) = '\0';
  30.         len = strlen(argv[2]);
  31.         if(*(argv[2]+len-1) == ';') *(argv[1]+len-1) = '\0';
  32.         pathname1 = strupr(argv[1]);
  33.         pathname2 = strupr(argv[2]);
  34.         path  = getenv("PATH");
  35.  
  36.         len = strlen(pathname1);
  37.         str = path;
  38.         while (str)    {
  39.             str = strstr(str, pathname1);
  40.             if(!str)    {
  41.                 /* pathname1 not found - just tag pathname2 on the end */
  42.                 writes(path,1);
  43.                 writes(";",1)
  44.                 writes(pathname2,1);
  45.                 break;
  46.             } else if(*(str+len) == ';' || *(str+len) == '\0')    {
  47.                 /* found it - insert pathname2 at the right place */
  48.                 char c = *str;
  49.                 *str = '\0';
  50.                 writes(path,1);
  51.                 writes(pathname2,1);
  52.                 writes(";",1);
  53.                 *str = c;
  54.                 writes(str,1);
  55.                 break;
  56.             } else    {
  57.                 /* incorrect match found - keep looking */
  58.                 str += len;
  59.             }
  60.         }
  61.     }
  62.     return(0);
  63. }
  64.  
  65.  
  66.